package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class UpdateVisitorServlet
 */
@WebServlet("/UpdateVisitorServlet")
public class UpdateVisitorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateVisitorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				
		response.setContentType("text/html");  
	    PrintWriter out = response.getWriter();  
	    String name=request.getParameter("name");  
	    String lastname=request.getParameter("lastname");
	    String dni=request.getParameter("dni");  
	    String username=request.getParameter("Username");
	    String password=request.getParameter("password");  
	    String email=request.getParameter("email");
	    int phone= Integer.parseInt(request.getParameter("phone"));  
	    String address=request.getParameter("adress");
		    
	    Visitor u=new Visitor();
		    u.setUsername(username);
		    u.setPassword(password);
		    u.setName(name);
		    u.setLastname(lastname);
		    u.setMail(email);
		    u.setDni(dni);
		    u.setAddress(address);
		    u.setPhone(phone);
	    VisitorService vs=new VisitorService();

	    Object iD = request.getSession().getAttribute("iDVisitor");
	    int id= (Integer) iD;
	 
try{
	    boolean tt =vs.updateVisitor(u, id);
	    if (tt==true){	
	    RequestDispatcher rd=request.getRequestDispatcher("mainpage2.jsp");  
	    rd.forward(request,response);
	    	
	    }
	    
	}catch(Exception ex){
		String msg= ex.getMessage();
		request.setAttribute("msg",msg); 
		RequestDispatcher dispatcher = request.getRequestDispatcher("mainpage.jsp");
        dispatcher.forward(request, response);
	}
	 
	 
	
	}
	

}
